package com.siyu.predict.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.siyu.basedata.rsvrrlaste.domain.RsvrRLaste;
import com.siyu.common.constant.PredictConstants;
import com.siyu.predict.vo.DrpVo;
import com.siyu.predict.vo.RsvrVo;
import org.springframework.stereotype.Component;

import javax.lang.model.type.ArrayType;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Component
public class DataServiceUtil {

    /**
     * 获取水位流量数据
     * @param rscd 水库编码
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param type 查询类型0=5分钟数据，1=小时数据，2=日数据
     * @return
     * @throws Exception
     */
    public static List<RsvrRLaste> getRsvrVoList(String rscd, Date startTime, Date endTime, Integer type) throws Exception {
        String url = PredictConstants.getIP_PORT() + "/monitordata/getWaterAndFlow";
        String token = PredictConstants.X_TOKEN;
        HashMap<String, Object> map = new HashMap<>();
        map.put("rscd", rscd);map.put("startTime", startTime);map.put("endTime", endTime);map.put("type", type);
//        String jsonString = HttpUtil.get(url, map);
        String jsonString = HttpRequest.get(url)
                .header("X-Token", token)
                .form(map)
                .execute()
                .body();
        JSONObject jsonObject = JSONUtil.parseObj(jsonString);
        JSONArray objArray = jsonObject.getJSONArray("obj");
        List<RsvrRLaste> rsvrRLastes = new ArrayList<>();
        if (objArray==null) return rsvrRLastes;
        for (Object obj : objArray) {
            if (obj instanceof JSONObject) {
                RsvrRLaste rsvrRLaste = new RsvrRLaste();
                JSONObject data = (JSONObject) obj;
                String tm = data.getStr("tm");
                rsvrRLaste.setTm(DateUtil.parse(tm));
                if (data.getStr("rz")!=null)rsvrRLaste.setRz(data.getDouble("rz"));
                if (data.getStr("inq")!=null)rsvrRLaste.setInq(data.getDouble("inq"));
                rsvrRLastes.add(rsvrRLaste);
            }
        }
        return rsvrRLastes;
    }

    /**
     * 获取降雨数据
     * @param rscd 水库编码
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param type 查询类型0=5分钟数据，1=小时数据，2=日数据
     * @return
     * @throws Exception
     */
    public static HashMap<Object,Object> getDrpVoList(String rscd, Date startTime, Date endTime, Integer type) throws Exception {
        HashMap<Object, Object> drphasmap = new HashMap<>();
        String url = PredictConstants.IP_PORT + "/monitordata/getRain";
        String token = PredictConstants.X_TOKEN;
        HashMap<String, Object> map = new HashMap<>();
        map.put("rscd", rscd);map.put("startTime", startTime);map.put("endTime", endTime);map.put("type", type);
//        String jsonString = HttpUtil.get(url, map);
        String jsonString = HttpRequest.get(url)
                .header("X-Token", token)
                .form(map)
                .execute()
                .body();
        JSONObject jsonObject = JSONUtil.parseObj(jsonString);
        JSONArray objArray = jsonObject.getJSONArray("obj");
        List<String> tms = new ArrayList<>();
        List<Double> areaRain = new ArrayList<>();
        if (objArray==null){
            drphasmap.put("tms", tms);
            drphasmap.put("areaRain", areaRain);
            return drphasmap;
        }
        for (Object obj : objArray) {
            if (obj instanceof JSONObject) {
                JSONObject data = (JSONObject) obj;
                if (data.getStr("drp")!=null){
                    tms.add(data.getStr("tm"));
                    areaRain.add(data.getDouble("drp"));
                }
            }
        }
        drphasmap.put("tms", tms);
        drphasmap.put("areaRain", areaRain);
        return drphasmap;
    }
}
